/**
 * 页面元素渲染工具类.
 */
define(['jquery','ecp.render.config'], function ($,renderConfig) {
	 var utils = {
			pageRender : function(options, callback, container, uiOptionFun){
	            if(typeof options === 'function'){
	                callback = options;
	                options = null;
	            }
	            if(typeof options === 'object'){
	                renderConfig = $.extend({}, renderConfig, options || {});
	            }
	            var allSelector = $("[class*='ecp-']", (container || $("body")) ),
	                len = allSelector.length,
	                deps = [],
	                map = {},
	                classNames,
	                currentKey,
	                i ;
	            for(i = 0; i < len; i++){
	                classNames = $(allSelector[i]).attr("class").split(" ");
	                for(var j = 0, cLen = classNames.length; j < cLen; j++){
	                    currentKey = renderConfig[classNames[j]];
	                    if(currentKey){
	                      if($.inArray(currentKey.moduleName, deps) === -1 ){
	                             deps.push(currentKey.moduleName);
	                         }
	                    }
	                }
	            }
	            require(deps, function(){
	            	var opt;
	                $.each(allSelector, function(index, element){
	                	 //不存在id的默认当前index
	                	 if($.isFunction(uiOptionFun)){
	                		 opt = uiOptionFun.call(this, element);
	                	 }
	                	 map[element.id || index] =  $(element).render(opt);
	                });
	                typeof callback === 'function' && callback(map);
	            },function(err){
	            	if(window.console){
	            		window.console.error(err.requireModules[0] + "模块加载出错.");
	            	}
	            });
			}
	 };
	 
	(function($){
        $.fn.render = function(option){
            var $this = $(this),
             	classNames = $this.attr("class").split(" "),
             	len = classNames.length,
                currentKey,
                ret,
                i;
            for(i = 0; i < len; i++){
                currentKey = renderConfig[classNames[i]];
                //多个不渲染
                if(currentKey && $this.length === 1){
                	if(typeof $this[currentKey.renderFn] === 'function'){
                		try{
                			var config = $.extend({}, $.parseJSON($this.attr("data-option")), typeof option ==="object" && option);
                    		ret = $this[currentKey.renderFn](config); 
                    		if(typeof option === 'string'){
                    			$this[currentKey.renderFn](option); 
                    		}
                		}catch(e){
                			window.console.error("渲染异常，配置信息如下："+$this.attr("data-option"));
                		}
                		break;
                	}
                }
            }
            return ret;
        }
    })(window.jQuery);
	 
	return utils;
});